/*********************************************************************************************************
Class Name: NMRUserSalesOffice
* Author: Accenture 
* Date: 2-April-2012  
* Requirement/Project Name: NMR 
* Requirement Description: To update the Timezone value on the User record based on the Value present against the salesoffice record.
*********************************************************************************************************/

public class NMRUserSalesOffice{
    @future    
    public static void nmrUserinsert(Set<Id> userid,Set<Id> NMRUserRoles){
    Map<string,List<User>> OfficeUserMap = new Map<string,List<User>>();
    Map<string,Sales_Office__c> OfficeMap = new Map<string,Sales_Office__c>();        
        
        for(User usr: [Select Id,css_office_name__c,UserRoleId from User where Id in:userId]){
        if(NMRUserRoles.contains(usr.UserRoleId)){
            
            if(!NMRUtil.isEmpty(usr.css_office_name__c)){
             if(OfficeUserMap.containsKey(usr.css_office_name__c)){
                 OfficeUserMap.get(usr.css_office_name__c).add(usr);
                 
             }
             else{                
                 OfficeUserMap.put(usr.css_office_name__c,new List<User>{usr});
                 
             }
           }
        }
    }
    if(OfficeUserMap.size()>0){
        for (Sales_Office__c ofc: [SELECT Id, Name,Time_Zone__c FROM Sales_Office__c WHERE Office_Code__c IN: OfficeUserMap.keyset()]) {
           
               for(User singuser:OfficeUserMap.get(ofc.Office_Code__c)){
                   //system.debug('@@@@@@@@@'+singuser.TimeZoneSidKey );
                   singuser.TimeZoneSidKey = ofc.Time_Zone__c;
               }
        }
    }        
    }
}